﻿namespace LxCore
{
    public class JsIdentifyItem : JsObject, IIdentity
    {
        protected string _Name = null;
        public virtual string Name
        {
            get => _Name;
            set
            {
                _Name = Parent is IIdentityList l ? FindValidName(l, value) : value;
                RaisePropertyChanged(nameof(Name));
            }
        }
        private int _ID = -1;
        public int ID
        {
            get => _ID;
            set { _ID = value; RaisePropertyChanged(nameof(ID)); }
        }
        private string PreValidName(string name)
        {
            for (int i1 = 0; i1 < name.Length; i1++)
            {
                if (isBadChar(name[i1]))
                {
                    return newName(i1);
                }
            }
            return name;
            string newName(int firstBadCharId)
            {
                char[] newName = new char[name.Length];
                int i1 = 0;
                for (; i1 < firstBadCharId; i1++)
                {
                    newName[i1] = name[i1];
                }
                int newId = firstBadCharId;
                i1++;
                for (; i1 < name.Length; i1++)
                {
                    if (isBadChar(name[i1])) { }
                    else
                    {
                        newName[newId] = name[i1];
                        newId++;
                    }
                }
                return new string(newName, 0, newId);
            }
            bool isBadChar(char ch)
            {
                switch (ch)
                {
                    case '\\':
                    case ':':
                        return true;
                    default:
                        return false;
                }
            }
        }
        public string FindValidName(IIdentityList list, string name)
        {
            if (name == null) name = Name;
            if (string.IsNullOrEmpty(name))
            {
                name = "新建名称";
            }
            else
            {
                name = PreValidName(name);
            }
            if (!list.HasSameName(name, this))
            {
                return name;
            }
            for (int i = 1; i < int.MaxValue; i++)
            {
                string newname = string.Format($"{name} {i}");
                if (!list.HasSameName(newname, this))
                {
                    return newname;
                }
            }
            throw new InvalidDataException("命名失败");
        }

        public override void FromJsonKV(string key, IJsonReader jsReader)
        {
            switch (key)
            {
                case nameof(Name): Name = jsReader.ReadString(); break;
                default: base.FromJsonKV(key, jsReader); break;
            }
        }
        public override void ToJsonKV(IJsonWriter jsWriter)
        {
            base.ToJsonKV(jsWriter);
            jsWriter.WriteString(nameof(Name), Name);
        }
    }

}
